import 'dart:async';
import 'package:flutter/material.dart';

class DanmuScreen extends StatefulWidget {
  @override
  _DanmuScreenState createState() => _DanmuScreenState();
}

class _DanmuScreenState extends State<DanmuScreen>
    with SingleTickerProviderStateMixin {
  List<String> danmus = [
    '弹幕1',
    '弹幕2',
    '弹幕3',
    '弹幕4',
    '弹幕5',
  ];
  late Timer timer;

  int currentIndex = 0;

  @override
  void initState() {
    super.initState();
    // 定时器，每隔2秒添加一条新的弹幕
    timer = Timer.periodic(const Duration(seconds: 2), (Timer t) {
      setState(() {
        if (currentIndex == danmus.length-1) {
          currentIndex = 0;
          debugPrint('上面下标$currentIndex--${danmus.length}');
        } else {
          currentIndex += 1;
          debugPrint('下面下标$currentIndex');
        }

      });
    });
  }

  @override
  void dispose() {
    timer.cancel(); // 销毁定时器
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(15),
            color: Colors.white,
          ),
          child: Row(
            children: [
              const SizedBox(
                width: 20,
              ),
              const Icon(Icons.search),
              const SizedBox(
                width: 10,
              ),
              SearchDanmuView(
                placeStr: danmus[currentIndex],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class SearchDanmuView extends StatefulWidget {
  String placeStr = '';

  SearchDanmuView({super.key, this.placeStr = ''});

  @override
  State<SearchDanmuView> createState() => _SearchDanmuViewState();
}

class _SearchDanmuViewState extends State<SearchDanmuView>
    with SingleTickerProviderStateMixin {
  late Animation<double> _animation;
  late AnimationController controller;

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
        animation: _animation,
        builder: (BuildContext context, Widget? child) {
          return Transform.translate(
            offset:  Offset(0, _animation.value*5),
            child: Text(
              widget.placeStr,
              style: const TextStyle(fontSize: 14),
            ),
          );
        });
  }

  @override
  void initState() {
    super.initState();
    controller =
        AnimationController(vsync: this, duration: const Duration(seconds: 2));
    _animation = Tween(
      begin: 1.0,
      end: -1.0,
    ).animate(
      CurvedAnimation(
        parent: controller,
        curve: Curves.linear,
      ),
    );
    controller.forward();

    controller.addStatusListener((status) {
     if(status==AnimationStatus.completed){
       controller.repeat();
     }
    });
  }

  @override
  void dispose() {
    if (controller != null) {
      if(controller.isAnimating){
        controller.stop();
        controller.dispose();
      }

    }
    super.dispose();

  }
}
